// pages/regist/regist.js
let app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    codename: '获取验证码', //按钮文字
    currentTime: 60, //倒计时
    disabled: false, //按钮是否禁用
    phoneNo: '', //获取到的手机栏中的值
    loading: false, 
    code: '', 
    password: '',
    rePassword: ''
  },


  onReady: function () {
    wx.request({
      url: app.serverUrl + '/getSessionId',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      method: 'POST',
      success: function (res) {
        wx.setStorageSync('sessionId', 'JSESSIONID=' + res.data.data)
        console.log(res.data.data)
      }
    })
  },
//  跳转到登录页
  goLogin: function() {
    wx.navigateTo({
      url: '/pages/login/login',
    })
  },

  // 获取手机栏input中的值
  phoneInput: function (e) {
    var phoneNo = e.detail.value;
    console.log('phoneNo' + phoneNo);
    this.setData({
      phoneNo: phoneNo
    })
    console.log('phone'+phoneNo);
  },
  //获验证码栏input中的值
  codeInput: function (e) {
    this.setData({
      code: e.detail.value
    })
  },
  passwordInput: function (e) {
    // console.log(e)
    this.setData({
      password: e.detail.value
    })
  },
  rePasswordInput: function(e) {
    this.setData({
      rePassword: e.detail.value
    })
  },
 

  //获取验证码按钮
  bindButtonTap: function () {
    var that = this;
    that.setData({
      disabled: true, //只要点击了按钮就让按钮禁用 （避免正常情况下多次触发定时器事件）
      color: '#ccc',
    })
    var phoneNo = this.data.phoneNo;
    var currentTime = this.data.currentTime //把手机号跟倒计时值变例成js值
    if (currentTime < 60) {
      return;
    }
    if (phoneNo == '') {
      wx.showToast({
        title: '请输入手机号',
        icon: 'none',
        duration: 2000
      });
      return;
    } else if(phoneNo.trim().length != 11 || !/^1[3|4|5|6|7|8|9]\d{9}$/.test(phoneNo)){
      wx.showToast({
        title: '手机号格式不正确',
        icon: 'none',
        duration: 2000
      });
      return;
    } else {
      //当手机号正确的时候提示用户短信验证码已经发送
      if (this.data.loading) {
        return;
      }
      wx.showLoading({
        title: '数据加载中',
      })
      this.setData({
        loading: true
      })
      var sessionId = wx.getStorageSync('sessionId');
      wx.request({
        url: app.serverUrl + '/sendMsg',
        data: {
          phoneNum: phoneNo
        },
        header: {
          "Content-Type": "application/x-www-form-urlencoded",
          "Cookie": sessionId
        },
        method: 'POST',
        success: function (res) {
          console.log(res.data);
          if (res.data.status == 200) {
            // 登录成功跳转 
            wx.showToast({
              title: '发送验证码成功',
              icon: 'success',
              duration: 2000
            });
            setTimeout(function () {
              wx.hideToast()
              currentTime--; //每执行一次让倒计时秒数减一
            }, 2000)

            that.setData({
              codename: currentTime + 's', //按钮文字变成倒计时对应秒数
              currentTime,
            })
            var interval = setInterval(function () {
              currentTime--; //每执行一次让倒计时秒数减一
              that.setData({
                codename: currentTime + 's', //按钮文字变成倒计时对应秒数
                currentTime,
              })
              if (currentTime <= 0) {
                clearInterval(interval)
                that.setData({
                  codename: '重新发送',
                  currentTime: 60,
                  disabled: false,
                  color: '#929fff'
                })
                setTimeout(function () {
                  wx.hideToast()
                }, 2000)
              }
            }, 1000);
           
          } else if (res.data.status == 500) {
            // 失败弹出框
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
              duration: 3000
            })
          }
        },
        
        complete: () => {
          wx.hideLoading()
          this.setData({
            loading: false
          })
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
  },

  check() {
    if (this.data.phoneNo == '') {
      wx.showToast({
        title: '请输入您的手机号',
        icon: 'none',
        duration: 2000
      });
      return false;
    } else if (!/^1[3|4|5|6|7|8|9]\d{9}$/.test(this.data.phoneNo)){
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none',
        duration: 2000
      });
      return false;
    }else if (this.data.code == '') {
      wx.showToast({
        title: '请输入验证码',
        icon: 'none',
        duration: 2000
      });
      return false;
    }else if (this.data.password <  8 || !(/^(\d|[a-zA-Z])*((\d[a-zA-Z])|([a-zA-Z]\d))(\d|[a-zA-Z])*$/.test(this.data.password))) {
      wx.showToast({
        title: '请输入符合要求的密码',
        icon: 'none',
        duration: 2000
      });
      return false;
    }else if (this.data.rePassword !== this.data.password) {
      wx.showToast({
        title: '两次密码不一致',
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    return true;
  },
  //注册
  Regist: function(e) {
    if (this.check()) {
      var phoneNum = this.data.phoneNo;
      var code = this.data.code;
      var password = this.data.password;
      console.log(phoneNum);
      console.log(code);
      console.log(password);
      var sessionId = wx.getStorageSync('sessionId');
      console.log(sessionId)
            wx.request({
              url: app.serverUrl + '/register',
              data: {
                code: code,
                userid: phoneNum,
                password: password,
              },
              method: 'POST',
              header: {
                "Content-Type": "application/x-www-form-urlencoded",
                "Cookie": sessionId
              },
              success: function (res) {
                console.log(res)
                if (res.data.status == 200) {
                  wx.showToast({
                    title: '注册成功',
                    icon: 'success',
                    duration: 2000
                  }) 
                  setTimeout(function () {
                    wx.hideToast()
                  }, 2000)
                  wx.reLaunch({
                    url: '/pages/login/login'
                  })
                } else if(res.data.status == 500) {
                  wx.showToast({
                    title: res.data.msg,
                    icon: 'fail'
                  })
              }
        }
      })

    }
  },


})